package com.ten.shou.service.impl;


import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ten.common.core.entity.EduCourse;
import com.ten.common.core.entity.TbZoneCourse;
import com.ten.common.core.entity.TblZone;
import com.ten.common.core.vo.Result;
import com.ten.shou.dao.ZoneCourseDao;
import com.ten.shou.dao.ZoneDao;
import com.ten.shou.feign.CourseListFeign;
import com.ten.shou.service.ZoneService;
import com.ten.shou.vo.ZoneId;
import com.ten.shou.vo.ZoneVo;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @author ：杨（1964958771@qq.com）
 * @date ：Created in 2022/12/7 16:23
 * @description：
 * @modified By：
 * @version:
 */
@Service
public class ZoneServiceImpl implements ZoneService {
    @Resource
    private ZoneDao zoneDao;

    @Resource
    private CourseListFeign courseListFeign;

    @Resource
    private ZoneCourseDao zoneCourseDao;

    @Override
    public Result<IPage<TblZone>> getAllZone(Integer current, Integer pageSize, ZoneVo zoneVo) {
        IPage<TblZone> page = new Page<>(current, pageSize);
        QueryWrapper<TblZone> wrapper = new QueryWrapper<>();
        if (StringUtils.hasText(zoneVo.getZoneName())) {
            wrapper.like("zone_name", zoneVo.getZoneName());
        }
        if (StringUtils.hasText(zoneVo.getIsDisable())) {
            wrapper.eq("is_Disable", zoneVo.getIsDisable());
        }
        IPage<TblZone> page1 = zoneDao.selectPage(page, wrapper);
        return new Result<>(2000, "查询成功", page1);
    }

    @Override
    public Result<TblZone> insertAllZone(TblZone tblZone) {
        if (ObjectUtil.isNotEmpty(tblZone.getId())) {
            int update = zoneDao.updateById(tblZone);
            if (update > 0) {
                return new Result<>(2000, "修改成功");
            }
        }
        zoneDao.insert(tblZone);
        return new Result<>(2000, "添加成功");
    }

    //    根据id删除专区信息
    @Override
    public Result deleteByIds(ZoneId zoneId) {
        zoneDao.deleteById(zoneId.getId());
        return new Result(2000, "删除成功");
    }

    @Override
    public Result<TblZone> updateStatus(String id, int isDisable) {
        UpdateWrapper wrapper = new UpdateWrapper<>();
        if (StringUtils.hasText(id)) {
            wrapper.eq("id", id);
        }
        if (StringUtils.hasText("isDisable")) {
            wrapper.set("is_disable", isDisable);
        }
        zoneDao.update(null, wrapper);
        return new Result(2000, "修改成功");
    }

    @Override
    public Result<List<EduCourse>> getAllQianZone() {
        QueryWrapper<TblZone> wrapper = new QueryWrapper<>();
        wrapper.eq("is_disable", "0");
        List<TblZone> tblZones = zoneDao.selectList(wrapper);
        //获取前端可以进行显示的所有课程
        List<EduCourse> courses = courseListFeign.getCoursesByConditions().getData();
        //设置每个专区应该显示的课程
        for (TblZone tblZone : tblZones) {
            List<EduCourse> list = new ArrayList<>();
            QueryWrapper<TbZoneCourse> wrapper1 = new QueryWrapper<>();
            wrapper1.eq("zone_id", tblZone.getId());
            List<TbZoneCourse> tbZoneCourses = zoneCourseDao.selectList(wrapper1);
            for (TbZoneCourse tbZoneCourse : tbZoneCourses) {
                for (EduCourse course : courses) {
                    if (tbZoneCourse.getCourseId().equals(course.getId())) {
                        list.add(course);
                    }
                }
            }
            System.out.println("dawfaf" + list);
            tblZone.setCourseList(list);
        }
        return new Result(2000, "查询课程管理中的课程成功", tblZones);
    }
}
